$(function(){
	
	/********************************************************************************
	TOP NAVIGATION  
	********************************************************************************/
	$(document).ready(function()
	{
		//cache nav
		$("#main_header_navigation ul li ul").each(function() {
		var $picker = $(this);
			$picker.bind('mouseenter',function() {
			    $picker.show();
			}).bind('mouseleave',function() {
			    $picker.hide();
			}).parent().bind('mouseenter',function() {
			    $picker.show();
			}).bind('mouseleave',function() {
			    $picker.hide();
			});
		});
	});
	/********************************************************************************
	LANGUAGE INDICATOR 
	********************************************************************************/
	// TODO FIXME: Check the function, does it work normaly ?
	$('.btn_flag').click(function()
	{

		//Get the lang
		var str  =  $(this).attr('id').split('_');
		var lang = str[2];

		//Deactivate all the flags
		$("#lang_indicator span").each(function(){
			$(this).attr('class','btn_flag');
		});
		
		//Activate the clicked flag		
		$(this).attr('class','btn_flag_active');
		
		
		//Remove the "default" class to the titles, intro and desc		
		$("#titles div[class='zone default'], #intros div[class='zone default'], #descriptions div[class='zone default'] ,#aliases div[class='zone default'] ,#metatages div[class='zone default'] ,#keywordes div[class='zone default']").each(function(){
			$(this).removeClass('default');
		});
		
		//Add the "default" class to the current language titles, intro and desc
		$("#zone_title_"+ lang + ", #zone_intro_"+ lang + ", #zone_desc_"+ lang + " , #zone_alias_" + lang + " , #zone_metatag_" + lang + " , #zone_keyword_"+ lang).each(function(){
			$(this).addClass('default');
			$(this).show();
		});

		//Hide all the "non default" div for titles, intro and desc 	
		$("#titles div[class='zone'], #intros div[class='zone'], #descriptions div[class='zone'] ,#aliases div[class='zone'] ,#metatages div[class='zone'] ,#keywordes div[class='zone']").each(function()
		{			
			$(this).hide();			
		});	
			
		//We don't forget to put class "closed" to the link that expand the blocs (because they all closed when we change language) 	
		$(".multi_language_expand").each(function()
		{			
			$(this).removeClass('opened').addClass('closed');			
		});	
		
	});	
		
	/********************************************************************************
	EXPAND OR REDUCES MULTI LANGUAGE BLOCS 
	********************************************************************************/
		
	$('.multi_language_expand').click(function()
	{

		//Get the category (titles, intro, desc, etc)
		var str  =  $(this).attr('id').split('_');
		var category = str[2];

		//Check the class of the link (opened or closed)
		if($(this).hasClass('closed'))
		{
			//Switch the opened class to the link	
			$(this).removeClass('closed').addClass('opened');
			
			//Try to display all the titles zones	
			$("#" + category + " div[class='zone']").each(function()
			{
				$(this).show();
			});
		}
		else
		{
			//Switch the opened class to the link		
			$(this).removeClass('opened').addClass('closed');
			
			//Try to display all the titles zones	
			$("#" + category + " div[class='zone']").each(function()
			{
				if(!$(this).hasClass('default'))
				{
					$(this).hide();
				}					
			});
		}
		
	});
	
	/********************************************************************************
	LEFT NAVIGATION ON ADMIN LAYOUT  
	********************************************************************************/
	// TODO FIXME: Check the function, do we need it ?
	$("span.icon").click(function(){
		if($(this).hasClass("icon-maximum"))
		{
			$("section nav").hide();
			 $("span.icon").removeClass("icon-maximum").addClass("icon-minimum");
			$("section").css("background-image","none");
			$("div.content").animate({
				"margin-left":0
			},250);
			$("span.icon").animate({
				"left":0
			},250);
		}else if($(this).hasClass("icon-minimum"))
		{
			$("div.content").animate({
				"margin-left":151
			},250);
			$("span.icon").removeClass("icon-minimum").addClass("icon-maximum");
			$("span.icon").animate({
				"left":192
			},250);
			$("section").attr("style","");
			$("section nav").show(250);
		}
	});
	
	/********************************************************************************
	EXPAND THE FIELDSET 
	********************************************************************************/
	
	$(".expand").click(function()
	{
		if ($(this).children('span').attr('class') == "icon-button mini")
		{
			$(this).children('span').attr('class','icon-button maxi');			
			$(this).next('div').hide("200");
		}
		else
		{
			$(this).children('span').attr('class','icon-button mini');			
			$(this).next('div').show("200");
		}		
		
	});
	
	/********************************************************************************
	TO CHANGE STATUS ALL FORM
	********************************************************************************/
	
	$('.status').click(function(){
		var list = $(this).children("span").attr('id');
		list = list.split("_");
		var tmp = $("<img>");
		tmp.attr("src","/images/admin/loader.gif");
		$(this).after(tmp);
		$.ajax({
			url:"/admin/parameter/changestatus/name/"+list[1]+"/id/"+list[2],
			cache:false,
			type:"POST",
			dataType:"text",
			success:function(data)
			{
				if(data == 1 )
					$("#status_"+list[1]+"_"+list[2]).attr('class','off');
				else
					$("#status_"+list[1]+"_"+list[2]).attr('class','on');
				tmp.remove();
			},
			error:function(data)
			{
				
			}
		});
	});
	
	/********************************************************************************
	TO CHANGE PRIORITY ALL FORM
	********************************************************************************/
	$(".help[name='enable_priority']").click(function(){
		var id = $(this).attr('id');
		id = id.split("_");
		if ($("#priority_"+id[2]+"_"+id[3]).attr('disabled'))
		{
			$("#priority_"+id[2]+"_"+id[3]).removeAttr('disabled');
			$("#icon_edit_priority_"+id[3]).attr('class','icon-button check');
			$("#icon_edit_priority_"+id[3]).next('span').html('OK');
		}
		else
		{
			$("#priority_"+id[2]+"_"+id[3]).attr('disabled','disabled');
			$("#icon_edit_priority_"+id[3]).attr('class','icon-button edit');
			$("#icon_edit_priority_"+id[3]).next('span').html('Edit Priority');
		}
	});
	
	$('select[name="priority"]').change(function(){
		var id = $(this).attr('id');
		id = id.split("_");
		var tmp = $("<img>");
		tmp.attr("src","/images/admin/loader.gif");
		$("#priority_"+id[1]+"_"+id[2]).next('a').hide();	
		$("#priority_"+id[1]+"_"+id[2]).after(tmp);
		$.ajax({
			url:"/admin/parameter/changepriority/name/"+id[1]+"/id/"+id[2]+"/priority/"+ $("#priority_"+id[1]+"_"+id[2]).val(),
			cache:false,
			type:"POST",
			dataType:"text",
			success:function(data)
			{
				tmp.remove();
				$("#priority_"+id[1]+"_"+id[2]).next('a').show();
				$("#priority_"+id[1]+"_"+id[2]).attr('disabled',"disabled");
				$("#icon_edit_priority_"+id[2]).attr('class','icon-button edit');
				$("#icon_edit_priority_"+id[2]).next('span').html('Edit Priority');
			},
			error:function(data)
			{
				
			}
		});
	});
	
	/********************************************************************************
	TO VALIDATION INPUT FORM
	********************************************************************************/
	$('#titleVi').validate_maxcharacter(255,'Maximum 255 Character');
	$('#titleEn').validate_maxcharacter(255,'Minimum 255 Character');
	$('#titleJa').validate_maxcharacter(255,'Minimum 255 Character');
	
	
	/********************************************************************************
	POPUP PEOPLES APLLY FOR CAREERS 
	********************************************************************************/
	$('a[name="career_popup"]').click(function(){
		var id = $(this).attr('id');
		id = id.split("_");
		id = "#"+id[0] +'_'+id[1]+"_"+"content_" + id[2];
		$(id).modal();
		return false;
	});
	
	/********************************************************************************
	TOOLTIPS INFOMATION FOR PEOPLES APLLY CAREERS 
	********************************************************************************/
	$("td.tooltip").hover(function(e){
		xOffset = 10;
		yOffset = 20;
		$content = $("#"+$(this).attr('name')).html();
		  
		$("body").append("<div id='tooltip_career'>"+$("#"+$(this).attr('name')).html()+"</div>");
		$("#tooltip_career")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("slow");
    },
	function(){
			
		$("#tooltip_career").remove();
    });	
	$("td.tooltip").mousemove(function(e){
		$("#tooltip_career")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});
});